package com.qingke.ssh.dao;

import java.util.List;

import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.qingke.ssh.bean.Employee;
import com.qingke.ssh.bean.EmployeeLogin;

@Repository
@Transactional
public class EmployeeDao {
	@Autowired
	private HibernateManager hibernateManager;

	public EmployeeLogin login(String username, String password) {
		System.out.println(hibernateManager);

		@SuppressWarnings("unchecked")
		Query<EmployeeLogin> query = hibernateManager.getSession()
				.createQuery("from EmployeeLogin where code='" + username + "' and password = '" + password + "'");
		if (query.getResultList().size() == 0) {
			return null;
		}
		return query.getResultList().get(0);
	}

	public Employee getEmployee(Employee employee, String phone) {
		@SuppressWarnings("unchecked")
		Query<Employee> query = hibernateManager.getSession()
				.createQuery("from Employee where phone = '" + phone + "'");
		System.out.println(query.getResultList().get(0).getId());
		return query.getResultList().get(0);
	}

	public Employee saveEmployee(Employee employee) {
		hibernateManager.save(employee);
		return employee;
	}

	public void saveEmployeeLogin(EmployeeLogin employeeLogin) {
		hibernateManager.save(employeeLogin);
	}

	public List<Employee> getAllEmployee() {
		Query<Employee> query = hibernateManager.getSession().createQuery("from Employee", Employee.class);
		return query.getResultList();
	}
}
